/*
 * @Date         : 2024-05-20 14:53:50 星期1
 * @Author       : xut
 * @Description  :
 */
import grpc from "@grpc/grpc-js"
import type { Request, Response } from "express"
import { postClient } from "../client"
import {
  GetPostPageInput,
  GetPostInput,
  UpdatePostInput,
  DeletePostInput,
  CreatePostInput,
} from "../schema/post.schema"
import { Post } from "../../pb/post/Post"

export async function getPostPage(
  req: Request<{}, {}, {}, GetPostPageInput>,
  res: Response
) {
  const headerMetadata = new grpc.Metadata()
  headerMetadata.set("user", JSON.stringify(req.user))

  const { pageNo, pageSize } = req.query
  const stream = postClient.listPosts(
    {
      page_no: pageNo,
      page_size: parseInt(pageSize),
    },
    headerMetadata
  )

  const postList: Post[] = []
  stream.on("data", (data: Post) => {
    postList.push(data)
  })
  stream.on("end", () => {
    return res.status(200).json({ status: "success", list: postList })
  })
  stream.on("error", (err: any) => {
    res.status(500).json({
      status: "error",
      message: err.message,
    })
  })
}

export async function getPost(
  req: Request<GetPostInput, {}, {}>,
  res: Response
) {
  const headerMetadata = new grpc.Metadata()
  headerMetadata.set("user", JSON.stringify(req.user))
  const postId = req.params.postId

  postClient.getPost({ id: postId }, headerMetadata, (err, data) => {
    if (err) {
      return res.status(400).json({
        status: "fail",
        message: err.message,
      })
    }

    return res.status(200).json(data)
  })
}

export async function createPost(
  req: Request<{}, {}, CreatePostInput>,
  res: Response
) {
  const headerMetadata = new grpc.Metadata()
  headerMetadata.set("user", JSON.stringify(req.user))

  const { title, content, category, published, image } = req.body
  postClient.CreatePost(
    { title, content, category, published, image },
    headerMetadata,
    (err, data) => {
      if (err) {
        return res.status(400).json({
          status: "fail",
          message: err.message,
        })
      }
      return res.status(200).json(data)
    }
  )
}

export async function updatePost(
  req: Request<UpdatePostInput["params"], {}, UpdatePostInput["body"]>,
  res: Response
) {
  const headerMetadata = new grpc.Metadata()
  headerMetadata.set("user", JSON.stringify(req.user))

  const postId = req.params.postId
  const { title, content, category, published, image } = req.body

  postClient.updatePost(
    {
      id: postId,
      title,
      content,
      category,
      published,
      image,
    },
    headerMetadata,
    (err, data) => {
      if (err) {
        return res.status(400).json({
          status: "fail",
          message: err.message,
        })
      }
      return res.status(200).json(data)
    }
  )
}

export async function deletePost(
  req: Request<DeletePostInput, {}, {}>,
  res: Response
) {
  const headerMetadata = new grpc.Metadata()
  headerMetadata.set("user", JSON.stringify(req.user))

  const postId = req.params.postId
  postClient.deletePost({ id: postId }, headerMetadata, (err, data) => {
    if (err) {
      return res.status(400).json({
        status: "fail",
        message: err.message,
      })
    }
    return res.status(200).json(data)
  })
}
